package com.share.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/*
一、ServletContext：
    1、域对象，在一定范围内（当前应用），使多个Servlet共享数据
    2、获取web.xml的全局配置信息，与ServletConfig的区别是，ServletConfig只能获取web.xml当前<servlet>的配置信息
    3、可以得到当前应用任何位置的文件的绝对路径getRealPath("fileName");
    4、可以实现请求的转发，HttpServletRequest也可以，一般用HttpServletRequest
    5、ServletContext的实现类是tomcat提供的  为：org.apache.catalina.core.ApplicationContextFacade.class
 */
public class ServletContext1 extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext application = getServletContext();
        application.setAttribute("name","sunxj");
        //org.apache.catalina.core.ApplicationContextFacade
        System.out.println("ServletContext的实现类为：" + application.getClass().getName());
//        test1(application);
//        test2(application);
//        test3(application);
//        test4(req, resp, application);
        test5(req, resp);
        System.out.println("finish");


    }

    private void test5(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //重定向：finish先于config2中name打印，因为client请求了两次
        System.out.println("contextPath: " + req.getContextPath());
        resp.sendRedirect(req.getContextPath()+"/config2");
    }

    private void test4(HttpServletRequest req, HttpServletResponse resp, ServletContext application) throws ServletException, IOException {
        //实现请求的转发:finish后与config2中的name打印，因为client请求了一次
        application.getRequestDispatcher("/config2").forward(req, resp);
    }

    private void test3(ServletContext application) throws IOException {
        //只能用idea-tomcat运行的时候可以看到
        String path = application.getRealPath("/WEB-INF/classes/a.properties");
        System.out.println("real-path: " + path);
        Properties properties = new Properties();
        properties.load(new FileInputStream(path));
        String key = properties.getProperty("key");
        System.out.println("key: " + key);
    }

    private void test2(ServletContext application) throws IOException {
        String path = application.getRealPath("/WEB-INF/b.properties");
        System.out.println("real-path: " + path);
        Properties properties = new Properties();
        properties.load(new FileInputStream(path));
        String key = properties.getProperty("key");
        System.out.println("key: " + key);
    }

    private void test1(ServletContext application) {
        //获取web.xml的全局配置信息
        String encoding = application.getInitParameter("encoding");
        if(null == encoding) {
            System.out.println("全局配置文件中没有该key");
        }
        System.out.println("encoding: " + encoding);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
